package com.third.clock.manager;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;

import com.orhanobut.logger.Logger;
import com.umeng.commonsdk.debug.E;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2018/7/25 0025.
 */

public class NetWorkManager implements IBaseManager{

    private Context mContext;

    private NetWorkBroadcastReceiver mNetWorkBroadcastReceiver;

    private OnNetWorkChaneObserve mOnNetWorkChaneObserve;

    private List<OnNetWorkChaneObserve> mObserves;

    private Handler mHandler=new Handler();

    public NetWorkManager(Context context){
        this.mContext=context;
        mObserves=new ArrayList<>();
    }

    @Override
    public void onCreate() {
        registerNetWork();
    }

    @Override
    public void onDestroy() {
        unregisterNetWork();
    }

    private void registerNetWork(){
        if(mNetWorkBroadcastReceiver==null){
            mNetWorkBroadcastReceiver=new NetWorkBroadcastReceiver();
        }
        IntentFilter intentFilter=new IntentFilter();
        intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        mContext.registerReceiver(mNetWorkBroadcastReceiver,intentFilter);
    }

    private void unregisterNetWork(){
        try{
           if(mNetWorkBroadcastReceiver!=null){
               mContext.unregisterReceiver(mNetWorkBroadcastReceiver);
           }
        }catch (Exception e){

        }
    }

    private void postOnChange(){
        if(mHandler!=null&&mRunnable!=null){
            mHandler.post(mRunnable);
        }
    }

    private Runnable mRunnable=new Runnable() {
        @Override
        public void run() {
            ConnectivityManager connMgr = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            boolean isWifiConn = networkInfo.isConnected();
            networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            boolean isMobileConn = networkInfo.isConnected();
            boolean isConnect=false;
            if(isWifiConn||isMobileConn){
                isConnect=true;
            }
            Logger.e("NetWorkManager  isConnect="+isConnect);
            pushOnNetWorkChaneObserves(isConnect);
        }
    };

    private class NetWorkBroadcastReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent==null) return;
            if(intent.getAction()==null) return;
            postOnChange();
        }
    }

    public interface OnNetWorkChaneObserve{
        void onNetWorkChane(boolean isConnect);
    }

    private void pushOnNetWorkChaneObserves(boolean isConnect){
        if(mObserves!=null){
            for (OnNetWorkChaneObserve observe:mObserves) {
                 if(observe!=null) observe.onNetWorkChane(isConnect);
            }
        }
    }

    public void registerOnNetWorkChaneObserve(OnNetWorkChaneObserve observe){
        mObserves.add(observe);
    }

    public void unregisterOnNetWorkChaneObserve(OnNetWorkChaneObserve observe){
        synchronized (mObserves){
            mObserves.remove(observe);
        }
    }

}
